package com.ty.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ty.mapper.ItemCatMapper;
import com.ty.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public void addItemCatList(ItemCat itemCat) {
        itemCat.setState(1);
        itemCatMapper.insert(itemCat);
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    public void updatestate(Integer id) {
        UpdateWrapper<ItemCat> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        ItemCat itemCat = new ItemCat();
        itemCat.setState(0);
        itemCatMapper.update(itemCat,updateWrapper);
    }
    public List<ItemCat> getwoList(Map<Integer,List<ItemCat>> map){
        List<ItemCat> onelist = map.get(0);
        for (ItemCat oneItemCat : onelist){
            List<ItemCat> twolist = map.get(oneItemCat.getId());
            oneItemCat.setChildren(twolist);
        }
        return onelist;
    }
    @Override
    public List<ItemCat> findItemCatList(ItemCat itemCat) {
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        boolean getName = StringUtils.hasLength(itemCat.getName());
        boolean getgetIslook;
        if (itemCat.getIsLook()==null){
            getgetIslook = false;
        }else {
            getgetIslook = true;
        }
        queryWrapper.and(Wrapper -> Wrapper.like(getName,"name",itemCat.getName())
                .eq(getgetIslook,"is_look", 1)
                .eq("state", 1));
        List<ItemCat> list = itemCatMapper.selectList(queryWrapper);
        for (ItemCat itemCatlist : list){ //判断是否已经有parentIdS
            int parentId = itemCatlist.getParentId();//获取父级ID
            itemCat.setLevel(itemCatlist.getLevel());
            if (map.containsKey(parentId)){
                List<ItemCat> exeList = map.get(parentId); //引用对象
                exeList.add(itemCatlist);
            }else{
                List<ItemCat> firstList = new ArrayList<>();
                firstList.add(itemCatlist);
                map.put(parentId, firstList);
            }
        }
        if (itemCat.getLevel() == 1){
            map.get(0);
        }
        if (itemCat.getLevel()  == 2){
            getwoList(map);
        }
        List<ItemCat> onelist = getwoList(map);
        for (ItemCat oneItemCat : onelist){
            List<ItemCat> twolist = oneItemCat.getChildren();
            if (twolist == null || twolist.size() == 0){
                continue;
            }
            for (ItemCat twoItemCat : twolist){
                List<ItemCat> threelist = map.get(twoItemCat.getId());
                twoItemCat.setChildren(threelist);
            }
            oneItemCat.setChildren(twolist);
        }
        return onelist;
    }

}
